<?php

namespace app\index\controller;

class Index
{
    public function index()
    {
        return view('index/index');
    }

    public function question()
    {
        return view('index/question');
    }

    public function share()
    {
        try{
            $jssdk = new \weixin\JSDK("wx5cd58f947a53db52", "06cef7b723db7f3ba9447dc7f700f34a");
            $signPackage = $jssdk->GetSignPackage();
            session("appId", $signPackage["appId"]);
            session("timestamp", $signPackage["timestamp"]);
            session("nonceStr", $signPackage["nonceStr"]);
            session("signature", $signPackage["signature"]);
        }catch(Exception $e){
            session("appId", "");
            session("timestamp", "");
            session("nonceStr", "");
            session("signature", "");
        }
       
        return view('index/share');
    }

    public function saveForm()
    {
        $request = request();
        if ($request->isPost()) {
            $username = $request->post("username");
            $mobile = $request->post("mobile");
            $province = $request->post("province");
            $shopId = $request->post("shopId");

            $consumerModel = model("Consumer");
            $data = [
                "name" => $username,
                "mobile" => $mobile,
                "province" => $province,
                "shop" => $shopId
            ];
            $res = $consumerModel->save($data);
            if ($res) {
                $res = [
                    "code" => 200,
                    "msg" => "提交成功",
                    "data" => "",
                ];
                return json($res);
            } else {
                $res = [
                    "code" => 500,
                    "msg" => "提交失败",
                    "data" => "",
                ];
                return json($res);
            }
        }
    }
}
